package com.it.schoolhomeos.repository;

import com.it.schoolhomeos.entity.Grade;
import com.it.schoolhomeos.entity.Student;
import com.it.schoolhomeos.entity.ClassInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 成绩数据访问接口
 * 
 * 提供对Grade实体的数据库操作方法
 * 继承JpaRepository以获取基本的CRUD操作
 */
@Repository
public interface GradeRepository extends JpaRepository<Grade, Long> {
    
    /**
     * 根据学生查找成绩记录
     * 
     * @param student 学生对象
     * @return 学生的成绩记录列表
     */
    List<Grade> findByStudent(Student student);
    
    /**
     * 根据学生分页查找成绩记录
     * 
     * @param student 学生对象
     * @param pageable 分页参数
     * @return 分页的成绩记录列表
     */
    Page<Grade> findByStudent(Student student, Pageable pageable);
    
    /**
     * 根据科目查找成绩记录
     */
    Page<Grade> findBySubject(String subject, Pageable pageable);
    
    /**
     * 根据学生和科目查找成绩记录
     */
    List<Grade> findByStudentAndSubject(Student student, String subject);
    
    /**
     * 根据学生和科目分页查找成绩记录
     */
    Page<Grade> findByStudentAndSubject(Student student, String subject, Pageable pageable);
    
    /**
     * 根据学生ID查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.id = :studentId")
    List<Grade> findByStudentId(@Param("studentId") Long studentId);
    
    /**
     * 根据学生ID分页查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.id = :studentId")
    Page<Grade> findByStudentIdPage(@Param("studentId") Long studentId, Pageable pageable);
    
    /**
     * 根据学生ID和科目查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.id = :studentId AND g.subject = :subject")
    List<Grade> findByStudentIdAndSubject(
            @Param("studentId") Long studentId, 
            @Param("subject") String subject);
    
    /**
     * 根据学生ID和科目分页查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.id = :studentId AND g.subject = :subject")
    Page<Grade> findByStudentIdAndSubjectPage(
            @Param("studentId") Long studentId, 
            @Param("subject") String subject,
            Pageable pageable);
    
    /**
     * 根据班级查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.classInfo = :classInfo")
    Page<Grade> findByStudentClassInfo(
            @Param("classInfo") ClassInfo classInfo, 
            Pageable pageable);
    
    /**
     * 根据班级和科目查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.classInfo = :classInfo AND g.subject = :subject")
    Page<Grade> findByStudentClassInfoAndSubject(
            @Param("classInfo") ClassInfo classInfo, 
            @Param("subject") String subject, 
            Pageable pageable);

    /**
     * 根据考试ID查找成绩记录
     */
    List<Grade> findByExamId(Long examId);
    
    /**
     * 根据考试ID分页查找成绩记录
     */
    Page<Grade> findByExamId(Long examId, Pageable pageable);
    
    /**
     * 根据考试ID和科目查找成绩记录
     */
    List<Grade> findByExamIdAndSubject(Long examId, String subject);
    
    /**
     * 根据考试ID和科目分页查找成绩记录
     */
    Page<Grade> findByExamIdAndSubject(Long examId, String subject, Pageable pageable);
    
    /**
     * 根据学生和考试ID查找成绩记录
     */
    List<Grade> findByStudentAndExamId(Student student, Long examId);
    
    /**
     * 根据学生和考试ID分页查找成绩记录
     */
    Page<Grade> findByStudentAndExamId(Student student, Long examId, Pageable pageable);
    
    /**
     * 根据学生、科目和考试ID查找成绩记录
     */
    List<Grade> findByStudentAndSubjectAndExamId(Student student, String subject, Long examId);
    
    /**
     * 根据学生、科目和考试ID分页查找成绩记录
     */
    Page<Grade> findByStudentAndSubjectAndExamId(Student student, String subject, Long examId, Pageable pageable);
    
    /**
     * 根据学生ID和考试ID查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.id = :studentId AND g.examId = :examId")
    List<Grade> findByStudentIdAndExamId(@Param("studentId") Long studentId, @Param("examId") Long examId);
    
    /**
     * 根据学生ID、考试ID和科目查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.id = :studentId AND g.examId = :examId AND g.subject = :subject")
    Optional<Grade> findByStudentIdAndExamIdAndSubject(
            @Param("studentId") Long studentId, 
            @Param("examId") Long examId,
            @Param("subject") String subject);
    
    /**
     * 根据班级和考试ID查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.classInfo = :classInfo AND g.examId = :examId")
    List<Grade> findByClassInfoAndExamId(
            @Param("classInfo") ClassInfo classInfo, 
            @Param("examId") Long examId);
    
    /**
     * 根据班级和考试ID分页查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.classInfo = :classInfo AND g.examId = :examId")
    Page<Grade> findByClassInfoAndExamId(
            @Param("classInfo") ClassInfo classInfo, 
            @Param("examId") Long examId,
            Pageable pageable);
    
    /**
     * 根据班级ID和考试ID查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.classInfo.id = :classId AND g.examId = :examId")
    List<Grade> findByClassIdAndExamId(
            @Param("classId") Long classId, 
            @Param("examId") Long examId);
    
    /**
     * 根据班级ID、考试ID和科目查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.classInfo.id = :classId AND g.examId = :examId AND g.subject = :subject")
    List<Grade> findByClassIdAndExamIdAndSubject(
            @Param("classId") Long classId, 
            @Param("examId") Long examId,
            @Param("subject") String subject);
    
    /**
     * 根据班级和科目查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.classInfo = :classInfo AND g.subject = :subject")
    Page<Grade> findByClassInfoAndSubject(
            @Param("classInfo") ClassInfo classInfo, 
            @Param("subject") String subject,
            Pageable pageable);
    
    /**
     * 根据班级查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.classInfo = :classInfo")
    Page<Grade> findByClassInfo(
            @Param("classInfo") ClassInfo classInfo,
            Pageable pageable);
    
    /**
     * 根据班级、科目和考试ID查找成绩记录
     */
    @Query("SELECT g FROM Grade g WHERE g.student.classInfo = :classInfo AND g.subject = :subject AND g.examId = :examId")
    Page<Grade> findByClassInfoAndSubjectAndExamId(
            @Param("classInfo") ClassInfo classInfo, 
            @Param("subject") String subject,
            @Param("examId") Long examId,
            Pageable pageable);

    /**
     * 根据科目和考试ID查找成绩记录
     */
    Page<Grade> findBySubjectAndExamId(String subject, Long examId, Pageable pageable);
} 